package com.xiaoyushu.count.biz.consumer;


import com.xiaoyushu.count.biz.constant.MQConstants;
import com.xiaoyushu.count.biz.constant.RedisKeyContsnts;
import com.xiaoyushu.count.biz.domain.mapper.UserCountDOMapper;
import com.xiaoyushu.count.biz.model.dto.NoteOperateMqDTO;
import com.xiaoyushu.framework.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@RocketMQMessageListener(consumerGroup = "xiaoyushu_group_"+ MQConstants.TOPIC_NOTE_OPERATE,//Group组
    topic = MQConstants.TOPIC_NOTE_OPERATE //主题 Topic
 )
@Slf4j
public class CountNotePublishConsumer implements RocketMQListener<Message> {

    private final RedisTemplate redisTemplate;
    private final UserCountDOMapper userCountDOMapper;

    public CountNotePublishConsumer(@Qualifier("redisTemplate") RedisTemplate redisTemplate, UserCountDOMapper userCountDOMapper) {
        this.redisTemplate = redisTemplate;
        this.userCountDOMapper = userCountDOMapper;
    }

    /**
     * @param message
     */
    @Override
    public void onMessage(Message message) {
        //消息体
        String bodyJsonStr = new String(message.getBody());
        //标签
        String tags = message.getTags();
        log.info("===> CountNotePublishConsumer 消费了消息{},tags:{}",bodyJsonStr,tags);

        //根据 MQ标签,判断笔记操作类型
        if (Objects.equals(tags,MQConstants.TAG_NOTE_PUBLISH)){ //笔记发布
            handleTagMessage(bodyJsonStr,1);
        } else if (Objects.equals(tags,MQConstants.TAG_NOTE_DELETE)) { //笔记删除
            handleTagMessage(bodyJsonStr,-1);
        }


    }

    /**
     * 笔记发布\删除
     * @param bodyJsonStr
     * @param count
     */
    private void handleTagMessage(String bodyJsonStr, long count) {
        //消息体 JSON 字符串 转DTO
        NoteOperateMqDTO noteOperateMqDTO = JsonUtils.parseObject(bodyJsonStr, NoteOperateMqDTO.class);

        if (Objects.isNull(noteOperateMqDTO)) return;

        //笔记发布者 ID
        Long creatorId = noteOperateMqDTO.getCreatorId();

        //更新 Redis 中用户维度的计数 Hash
        String countUserKey = RedisKeyContsnts.buildCountUserKey(creatorId);
        //判断 Redis 中Hash 是否存在
        Boolean isCountUserExisted = redisTemplate.hasKey(countUserKey);
        //若存在才会更新
        //(因为缓存设有过期时间,考虑到过去后,缓存会被删除,这里需要判断一下,存在才会去更新,而初始化工作放在查询计数来做)
        if (isCountUserExisted){
            //对目标用户Hash 中的笔记发布总数,进行加减操作
            redisTemplate.opsForHash().increment(countUserKey,RedisKeyContsnts.FIELD_NOTE_TOTAL,count);
        }

        //更新 t_user_count 表
        userCountDOMapper.insertOrUpdateNoteTotalByUserId(count,creatorId);

    }

    /**
     * 笔记删除
     * @param bodyJsonStr
     */
    private void handleNoteDeleteTagMessage(String bodyJsonStr) {
    }

    /**
     * 笔记发布
     * @param bodyJsonStr
     */
    private void handleNotePublishTagMessage(String bodyJsonStr) {
    }
}
